package com.blog.controller;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.blog.entity.Blog;
import com.blog.entity.TagAndTotal;
import com.blog.service.BlogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class BlogController {

	@Autowired BlogService blogService;
	
	@RequestMapping("/")
	public String index(HttpServletRequest request){
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		if(currentUser.getPrincipal() != null && session.getAttribute("userName") == null){
			String principal = currentUser.getPrincipal().toString();
			session.setAttribute("userName", principal);
		}
		
		session.removeAttribute("tagName");
		return "index";
	}
	
	@RequestMapping("createblog")
	public String addblog(HttpServletRequest request){
		return "addblog";
	}
	@RequestMapping("editblog")
	public String editblog(HttpServletRequest request){
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();  
		session.setAttribute("blogId", request.getParameter("blogId"));
		return "editblog";
	}
	@RequestMapping("content")
	public String content(HttpServletRequest request){
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();  
		session.setAttribute("blogId", request.getParameter("blogId"));
		return "content";
	}
	@RequestMapping("list")
	public String list(HttpServletRequest request){
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();  
		session.setAttribute("tagName", request.getParameter("tagName"));
		return "list";
	}
	@RequestMapping("setTagName")
	public void setTagName(HttpServletRequest request){
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();  
		session.setAttribute("tagName", request.getParameter("tagName"));
	}
	@RequestMapping("goToPageByAllBlog")
	public void goToPageByAllBlog(HttpServletRequest request){
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();  
		session.setAttribute("goToPage", request.getParameter("gotoPage"));
	}
	@RequestMapping("ueditor")
	public String ueditor(HttpServletRequest request){
		return "ueditor";
	}
	
	@RequestMapping("addblog")
	@ResponseBody
	public JSONObject addBlog(@RequestBody Blog blog){
		JSONObject resMsg = new JSONObject();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		blog.setPublishT(sdf.format(new Date()));// new Date()为获取当前系统时间
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();  
		String userName = (String)session.getAttribute("userName");
		blog.setAuthor(userName);
		int res = blogService.createBlog(blog);
		
		resMsg.put("res", res);
		return resMsg;
	}
	
	@RequestMapping("getBolgs")
	@ResponseBody
	public List<Map<String,Object>> getBolgs(HttpServletRequest request){
		int rqpage = Integer.parseInt(request.getParameter("rqpage"));
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession(); 
		//String userName = currentUser.getPrincipal().toString(); 
		
		String userName = (String)session.getAttribute("userName");
		Map<String,Object> paramObject = new HashMap<>();
		paramObject.put("userName", userName);
		
		List<Map<String,Object>> blogs = new ArrayList<>();
		List<Map<String,Object>> blogs2 = new ArrayList<>();
		//页数
		//int page = (rqpage - 1) * 5;
		//paramObject.put("page", page);
		//首页没有分类
		paramObject.put("tag", null);
		
		blogs = blogService.getBolgs(paramObject);
		for (int i = 0; i < blogs.size(); i++) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss"); 
			Timestamp timeStr = (Timestamp)blogs.get(i).get("publishT");
			String publishT = sdf.format(timeStr);  
			blogs.get(i).put("publishT", publishT);
		}
		//静态方法调用分页处理
		PageHelper.startPage(rqpage, 5);
		blogs2 = blogService.getBolgs(paramObject);
		PageInfo<Map<String,String>> page = new PageInfo(blogs2);
		for (int i = 0; i < blogs2.size(); i++) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss"); 
			Timestamp timeStr = (Timestamp)blogs2.get(i).get("publishT");
			String publishT = sdf.format(timeStr);  
			blogs2.get(i).put("publishT", publishT);
		}
		//保存分页信息
		PageInfo pageInfo = new PageInfo(blogs2);
		System.out.println(blogs2);
		System.out.println(pageInfo);
		return blogs2;
	}
	@RequestMapping("listGetBolgs")
	@ResponseBody
	public List<Map<String,Object>> listGetBolgs(HttpServletRequest request){
		int rqpage = Integer.parseInt(request.getParameter("rqpage"));
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();  
		String userName = (String)session.getAttribute("userName");
		String tagName = (String)session.getAttribute("tagName");
		Map<String,Object> paramObject = new HashMap<>();
		paramObject.put("userName", userName);
		
		List<Map<String,Object>> blogs = new ArrayList<>();
		
		//页数
		int page = (rqpage - 1) * 5;
		paramObject.put("page", page);
		//列表页获取session中的tagName
		paramObject.put("tag", tagName);
		
		PageHelper.startPage(rqpage, 5);
		blogs = blogService.getBolgs(paramObject);
		for (int i = 0; i < blogs.size(); i++) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss"); 
			Timestamp timeStr = (Timestamp)blogs.get(i).get("publishT");
			String publishT = sdf.format(timeStr);  
			blogs.get(i).put("publishT", publishT);
		}
		return blogs;
	}
	
	@RequestMapping("centerSetTag")
	public void centerSetTag(HttpServletRequest request){
		String tagName = request.getParameter("tagName");
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();  
		session.setAttribute("tagNameC", tagName);
	}
	@RequestMapping("centerGetTag")
	@ResponseBody
	public String centerGetTag(HttpServletRequest request){
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();  
		String res = (String)session.getAttribute("tagNameC");
		if(res == null){
			res = "all";
		}
		return res;
	}
	@RequestMapping("centerGetBolgs")
	@ResponseBody
	public List<Map<String,Object>> centerGetBolgs(HttpServletRequest request){
		int rqpage = Integer.parseInt(request.getParameter("rqpage"));
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();  
		String userName = (String)session.getAttribute("userName");
		String tag = (String)session.getAttribute("tagNameC");
		Map<String,Object> paramObject = new HashMap<>();
		paramObject.put("userName", userName);
		
		List<Map<String,Object>> blogs = new ArrayList<>();
		
		//页数
		int page = (rqpage - 1) * 5;
		paramObject.put("page", page);
		//判断tagName
		if(tag == null || tag.equals("all")){
			paramObject.put("tag", null);
		}
		else{
			paramObject.put("tag", tag);
		}
		PageHelper.startPage(rqpage, 5);
		blogs = blogService.getBolgs(paramObject);
		for (int i = 0; i < blogs.size(); i++) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss"); 
			Timestamp timeStr = (Timestamp)blogs.get(i).get("publishT");
			String publishT = sdf.format(timeStr);  
			blogs.get(i).put("publishT", publishT);
		}
		return blogs;
	}
	
	@RequestMapping("getallblogtotal")
	@ResponseBody
	public String getAllBlogTotal(){
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();  
		String userName = (String)session.getAttribute("userName");
		String total = blogService.getAllBlogTotal(userName);
		return total;
	}
	
	@RequestMapping("getBlog.do")
	@ResponseBody
	public Blog getBlog(){
		Subject currenUser = SecurityUtils.getSubject();
		Session session = currenUser.getSession();
		int blogId = Integer.parseInt((String)session.getAttribute("blogId"));
		Blog blog = blogService.getBolg(blogId);
		SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss"); 
		Timestamp timeStr = Timestamp.valueOf(blog.getPublishT());
		String publishT = sdf.format(timeStr);  
		blog.setPublishT(publishT);
		blog.setContent(StringEscapeUtils.unescapeHtml(blog.getContent()));
		return blog;
	}
	
	@RequestMapping("saveblog")
	@ResponseBody
	public JSONObject saveblog(@RequestBody Blog blog){
		JSONObject resMsg = new JSONObject();
		/*Blog blog = new Blog();
		blog.setTitle(request.getParameter("title"));
		blog.setTag(request.getParameter("tag"));
		blog.setContent(request.getParameter("centent"));*/
		Subject currenUser = SecurityUtils.getSubject();
		Session session = currenUser.getSession();
		int blogId = Integer.parseInt((String)session.getAttribute("blogId"));
		
		blog.setId(blogId);
		int res = blogService.saveBlog(blog);
		
		session.removeAttribute("blogId");
		resMsg.put("res", res);
		return resMsg;
	}
	
	@RequestMapping("delBlog")
	@ResponseBody
	public JSONObject delBlog(HttpServletRequest request){
		JSONObject resMsg = new JSONObject();
		int blogId = Integer.parseInt(request.getParameter("blogId"));
		int res = blogService.delBlog(blogId);
		resMsg.put("res", res);
		return resMsg;
	}
	
	@RequestMapping("getTags")
	@ResponseBody
	public List<Map<String,String>> getTags(){
		List<Map<String,String>> resList = new ArrayList<>();
		Map<String,String> paramMap = new HashMap<>();
		String total;
		
		Subject currenUser = SecurityUtils.getSubject();
		Session session = currenUser.getSession();
		String userName = session.getAttribute("userName").toString();
		Set<String> tags = blogService.selectTags(userName);
		paramMap.put("userName", userName);
		for(String tag : tags){
			paramMap.put("tagName", tag);
			total = blogService.getTagTotal(paramMap).toString();
			Map<String,String> taginfo = new HashMap<>();
			taginfo.put("tagName",tag);
			taginfo.put("total",total);
			resList.add(taginfo);
		}
		return resList;
	}
	
	@RequestMapping("listGetTagTotalByUserNameAndTag")
	@ResponseBody
	public String listGetTagTotalByUserNameAndTag(){
		Map<String,String> map = new HashMap<>();
		String res = "";
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();  
		String userName = (String)session.getAttribute("userName");
		String tagName = (String)session.getAttribute("tagName");
		map.put("userName", userName);
		map.put("tagName", tagName);
		res = blogService.getTagTotalByUserNameAndTag(map);
		return res;
	}
	
	@RequestMapping("getTagTotalByUserNameAndTag")
	@ResponseBody
	public String getTagTotalByUserNameAndTag(){
		Map<String,String> map = new HashMap<>();
		String res = "";
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();  
		String userName = (String)session.getAttribute("userName");
		String tagName = (String)session.getAttribute("tagNameC");
		map.put("userName", userName);
		map.put("tagName", tagName);
		res = blogService.getTagTotalByUserNameAndTag(map);
		return res;
	}
}
